﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Collections;
using System.ComponentModel;

namespace Ict.DataAccess
{
    /// <summary>
    /// 数据实体列表类
    /// </summary>
    /// <typeparam name="T">实体类型，必须是DataEntity或其子类</typeparam>
    public class DataList<T> : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerator<T>, ICollection
        where T : DataEntity, new()
    {
        private DataView _dataView = null;
        
        private IEnumerator _enumerator = null;

        private Dictionary<DataRow, T> _objectSet = new Dictionary<DataRow, T>();

        /// <summary>
        /// 用dataTable初始化DataList
        /// </summary>
        /// <param name="dataTable"></param>
        public DataList(DataView dataView)
        {
            _dataView = dataView;
        }

        ~DataList()
        {
            _dataView.Dispose();
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int Count
        {
            get
            {
                if (_dataView != null)
                    //return _dataTable.Rows.Count;
                    return _dataView.Count;
                else
                    return 0;
            }
        }

        #region IBindingList Members

        public void Add(T value)
        {
            throw new NotImplementedException();
        }

        public void AddIndex(PropertyDescriptor property)
        {
            throw new NotImplementedException();
        }

        public object AddNew()
        {
            DataRow row = _dataView.Table.NewRow();
            T t = new T();
            t.DataRow = row;
            return t;
        }

        public void ApplySort(PropertyDescriptor property, ListSortDirection direction)
        {
            throw new NotImplementedException();
        }

        public void Clear()
        {
            _dataView.Table.Rows.Clear();
        }

        public bool Contains(T value)
        {
            throw new NotImplementedException();
        }

        public void CopyTo(T[] array, int index)
        {
            if (null == array)
                throw new ArgumentNullException();
            if (index < 0)
                throw new ArgumentOutOfRangeException();
            if (array.Rank > 1 ||
                array.Length <= index ||
                this.Count > array.Length - index)
                throw new ArgumentException();

            IEnumerator<T> enumerator = this.GetEnumerator();

            for (int i = index; i < array.Length && enumerator.MoveNext(); i++)
            {
                array[i] = enumerator.Current;
            }

        }

        public void CopyTo(Array array, int index)
        {
            if (null == array)
                throw new ArgumentNullException();
            if (index < 0)
                throw new ArgumentOutOfRangeException();
            if (array.Rank > 1 ||
                array.Length <= index ||
                this.Count > array.Length - index)
                throw new ArgumentException();

            IEnumerator<T> enumerator = this.GetEnumerator();

            for (int i = index; i < array.Length && enumerator.MoveNext(); i++)
            {
                array.SetValue(enumerator.Current, i);
            }
        }

        public int Find(PropertyDescriptor property, Object key)
        {
            throw new NotImplementedException();
        }

        public int IndexOf(T value)
        {
            throw new NotImplementedException();
        }

        public void Insert(int index, T value)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 从集合中移除指定的 System.Data.DataRow。
        /// </summary>
        /// <param name="value"></param>
        public bool Remove(T value)
        {
            if (value == null)
                return false;
            _dataView.Table.Rows.Remove(value.DataRow);
            Reset();
            return true;
        }
        /// <summary>
        /// 从集合中移除指定索引处的行。
        /// </summary>
        /// <param name="index"></param>
        public void RemoveAt(int index)
        {
            if (index >= 0)
                _dataView[index].Delete();
        }

        public void RemoveIndex(PropertyDescriptor property)
        {
            throw new NotImplementedException();
        }

        public void RemoveSort()
        {
            throw new NotImplementedException();
        }

        public bool AllowEdit
        {
            get
            {
                return true;
            }
        }

        public bool AllowNew
        {
            get
            {
                return true;
            }
        }

        public bool AllowRemove
        {
            get
            {
                return true;
            }
        }

        public bool IsFixedSize
        {
            get
            {
                return false;
            }
        }

        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }

        public bool IsSorted
        {
            get
            {
                return true;
            }
        }

        public bool IsSynchronized
        {
            get
            {
                return true;
            }
        }

        public T this[int index]
        {
            get
            {
                DataRow row = _dataView[index].Row;
                if (_objectSet.ContainsKey(row))
                    return _objectSet[row];
                else
                {
                    T t = new T();
                    t.DataRow = row;
                    _objectSet.Add(row, t);
                    return t;
                }
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public ListSortDirection SortDirection
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        public PropertyDescriptor SortProperty
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        public bool SupportsChangeNotification
        {
            get
            {
                return false;
            }
        }

        public bool SupportsSearching
        {
            get
            {
                return true;
            }
        }

        public bool SupportsSorting
        {
            get
            {
                return true;
            }
        }

        public object SyncRoot
        {
            get
            {
                return _dataView.Table.Rows.SyncRoot;
            }
        }

        public event ListChangedEventHandler ListChanged
        {
            add
            {
                throw new NotImplementedException();
            }
            remove
            {
                throw new NotImplementedException();
            }
        }

        #endregion IBindingList Members

        /// <summary>
        /// 实现IEnumerable接口
        /// </summary>
        #region IEnumerable Members

        public IEnumerator<T> GetEnumerator()
        {
            Reset();
            return this as IEnumerator<T>;
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return (this as IEnumerable<T>).GetEnumerator();
        }

        #endregion

        /// <summary>
        /// 实现IEnumerator接口
        /// </summary>
        #region IEnumerator Members

        public void Reset()
        {
            // TODO:  Add CStringEnum.Reset implementation
            _enumerator = _dataView.GetEnumerator();
        }

        public T Current
        {
            get
            {
                // TODO:  Add CStringEnum.Current getter implementation
                DataRow row = (_enumerator.Current as DataRowView).Row;
                if (_objectSet.ContainsKey(row))
                    return _objectSet[row];
                else
                {
                    T t = new T();
                    t.DataRow = row;
                    _objectSet.Add(row, t);
                    return t;
                }
            }
        }

        object IEnumerator.Current
        {
            get
            {
                return (this as IEnumerator<T>).Current;
            }
        }

        public bool MoveNext()
        {
            // TODO:  Add CStringEnum.MoveNext implementation
            return _enumerator.MoveNext();
        }

        #endregion

        /// <summary>
        /// 实现IDisposable
        /// </summary>
        #region IDisposable Members

        public void Dispose()
        {
            //_dataView.Dispose();
            //GC.SuppressFinalize(this);
        }

        #endregion
    }
}
